package org.example.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@RequestMapping("/api")
public class VideoController {

    // 视频文件的存储路径，需要根据实际情况修改
//    private static final String VIDEO_FILE_PATH = "./data/vedio.mp4";
    private static final String VIDEO_FILE_PATH = "src/main/java/org/example/controller/data/test2.mp4";
    @GetMapping("/video")
    public ResponseEntity<Resource> getVideo() {
        File videoFile = new File(VIDEO_FILE_PATH);
        Resource videoResource = new FileSystemResource(videoFile);

        if (!videoResource.exists()) {
            return ResponseEntity.notFound().build();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=video.mp4");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(videoFile.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(videoResource);
    }
}